﻿String.prototype.Trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
$(function() {
    //--------------------建筑处理------------------
    $("#btnAddJZ1").bind("click", function() {

        var front = "";
        var last = "";
        var max = 0;
        var count = 0;
        //获取最大id
        $("tr[id^='jztr']").each(function() {
            front = $(this).attr("id");
            last = front.toString().substr(4, front.length - 4);
            if (Number(last) > max) {
                max = last;
            }
            count++;
        });
        $("a[id*='btnDelJZ']").show();
        var newid = Number(max) + 1;
        var lastNumber = count + 1;
        $("#tableJZ tr:nth-child(" + lastNumber + ")").after($("#jztr" + max).clone().attr("id", "jztr" + newid));
        $("#jztr" + newid + " input").each(function() {
            $(this).val(""); //清空数据
            $(this).css("background-color", "");
        })
        $("#jztr" + newid + " select").each(function() {
            $(this).get(0).selectedIndex = 0; //初始
            $(this).css("background-color", "");
        })
        $("#jztr" + newid + " a").each(function() {
            var count = 0;
            $(this).attr("id", "btnDelJZ" + newid);
            $(this).bind("click", function() {
                $("tr[id^='jztr']").each(function() {
                    count++;
                });
                if (count > 1) {
                    if (count == 2) {
                        $("a[id*='btnDelJZ']").each(function() {
                            $(this).hide();
                        })
                    }
                    $("#jztr" + newid).remove();
                }
            })
        })
    });
    $("a[id*='btnDelJZ']").each(function() {
        $(this).bind("click", function() {
            var count = 0;
            $("tr[id^='jztr']").each(function() {
                count++;
            });
            if (count > 1) {
                if (count == 2) {
                    $("a[id*='btnDelJZ']").each(function() {
                        $(this).hide();
                    })
                }
                $(this).parent().parent().remove();
            }
        });
    });

    $("input[type='checkbox'][id*='cbhh']").bind("click", function() {
        if ($(this).attr("checked") != "") {
            var subid = $(this).attr("id").toString().substr(22);
            var cbxx = "#ctl00_MainContent_cbxx" + subid;
            $(cbxx).attr("checked", "checked");
        }
    })

    $("input[type='checkbox'][id*='cbbb']").bind("click", function() {
        if ($(this).attr("checked") != "") {
            var subid = $(this).attr("id").toString().substr(22);
            var cbxx = "#ctl00_MainContent_cbxx" + subid;
            $(cbxx).attr("checked", "checked");
        }
    })
});

function GetEventSave() {
    var yanzheng = true;
    if (yanzheng == true) {
        yanzheng = YzRequired();
    }
    if (yanzheng == true) {
        yanzheng = jzdata(); //验建筑
    }
    if (yanzheng == true) {
        yanzheng = PDJL();
    }
    if (yanzheng == true) {
        if ($("#ctl00_MainContent_cbhg").attr("checked") == "" && $("#ctl00_MainContent_cbbhg").attr("checked") == "") {
            yanzheng = false;
            $("#ctl00_MainContent_cbhg").parent().css("background-color", "plum");
            $("#ctl00_MainContent_cbbhg").parent().css("background-color", "plum");
            alert("请选择检查结果");
        }
        else {
            $("#ctl00_MainContent_cbhg").parent().css("background-color", "");
            $("#ctl00_MainContent_cbbhg").parent().css("background-color", "");
        }
    }
    if (yanzheng == true) {
        yanzheng = yzqj();
    }
    if (yanzheng == true) {
        yanzheng = ajaxyzwh();
    }
    return yanzheng;
}

function yzqj() {
    var yanzheng = true;
    var hegesum = 0;
    var xzhegesum = 0;
    $("input[type='checkbox'][id*='cbhh']").each(function() {
        var subid = $(this).attr("id").toString().substr(22);
        var cbxx = "#ctl00_MainContent_cbxx" + subid;
        if ($(cbxx).attr("checked") != "") {
            hegesum++;
        }
        if ($(this).attr("checked") != "") {
            xzhegesum++;
        }

    });
    if (hegesum == xzhegesum) {
        if ($("input[id='ctl00_MainContent_cbhg']").attr("checked") == "") {
            if (hegesum > 0) {
                $("#ctl00_MainContent_cbhg").parent().css("background-color", "plum");
                $("#ctl00_MainContent_cbbhg").parent().css("background-color", "plum");
                yanzheng = false;
                alert("评定结论都是合格，检查结果必定合格");
            }
        }
        else {
            $("#ctl00_MainContent_cbhg").parent().css("background-color", "");
            $("#ctl00_MainContent_cbbhg").parent().css("background-color", "");
        }
    }
    hegesum = 0;
    xzhegesum = 0;
    $("input[type='checkbox'][id*='cbbb']").each(function() {
        var subid = $(this).attr("id").toString().substr(22);
        var cbxx = "#ctl00_MainContent_cbxx" + subid;
        if ($(cbxx).attr("checked") != "") {
            hegesum++;
        }
        if ($(this).attr("checked") != "") {
            xzhegesum++;
        }
    });
    if (hegesum == xzhegesum) {
        if ($("input[id='ctl00_MainContent_cbbhg']").attr("checked") == "") {
            if (hegesum > 0) {
                $("#ctl00_MainContent_cbhg").parent().css("background-color", "plum");
                $("#ctl00_MainContent_cbbhg").parent().css("background-color", "plum");
                yanzheng = false;
                alert("评定结论都是不合格，检查结果必定不合格");
            }
        }
        else {
            $("#ctl00_MainContent_cbhg").parent().css("background-color", "");
            $("#ctl00_MainContent_cbbhg").parent().css("background-color", "");
        }
    }
    return yanzheng;
}

function YzRequired() {
    var yanzheng = true;
    var message = "";
    if ($("#ctl00_MainContent_txtGcmc").val().Trim() == "") {
        message = message + "建设工程名称不能为空" + "\n";
        $("#ctl00_MainContent_txtGcmc").css("background-color", "plum");
    }
    else {
        $("#ctl00_MainContent_txtGcmc").css("background-color", "");
    }
    if ($("#ctl00_MainContent_txtGcdz").val().Trim() == "") {
        message = message + "建设工程名称不能为空" + "\n";
        $("#ctl00_MainContent_txtGcdz").css("background-color", "plum");
    }
    else {
        $("#ctl00_MainContent_txtGcdz").css("background-color", "");
    }
    if ($("#ctl00_MainContent_txtJcrq").val().Trim() == "") {
        message = message + "检查日期不能为空" + "\n";
        $("#ctl00_MainContent_txtJcrq").css("background-color", "plum");
    }
    else {
        $("#ctl00_MainContent_txtJcrq").css("background-color", "");
    }
    if ($("#ctl00_MainContent_txtJsdwmc").val().Trim() == "") {
        message = message + "建设单位名称不能为空" + "\n";
        $("#ctl00_MainContent_txtJsdwmc").css("background-color", "plum");
    }
    else {
        $("#ctl00_MainContent_txtJsdwmc").css("background-color", "");
    }
    if ($("#ctl00_MainContent_txtPdjl").val().Trim() == "") {
        message = message + "竣工验收消防检查基本情况和综合评定结论不能为空" + "\n";
        //$("#ctl00_MainContent_txtPdjl").css("background-color", "plum");
    }
    else {
        if ($("#ctl00_MainContent_txtPdjl").val().Trim().length > 330) {
            message = message + "竣工验收消防检查基本情况和综合评定结论超过限定字数" + "\n";
        }
        else {
            $("#ctl00_MainContent_txtPdjl").css("background-color", "");
        }
    }
    if ($("#ctl00_MainContent_txtJcry").val().Trim() == "") {
        message = message + "竣工验收消防检查人员不能为空" + "\n";
        $("#ctl00_MainContent_txtJcry").css("background-color", "plum");
    }
    else {
        $("#ctl00_MainContent_txtJcry").css("background-color", "");
    }
    if (message != "") {
        yanzheng = false;
        alert(message);
    }
    return yanzheng;
}
//验评定结论
function PDJL() {
    var yanzheng = true;
    $("input[id*='cbxx'][type='checkbox']").each(function() {
        var id = $(this).attr("id").toString();
        var hhid = "input[id$='cbhh" + id.substr(id.length - 2, 2) + "'][type='checkbox']";
        var bbid = "input[id$='cbbb" + id.substr(id.length - 2, 2) + "'][type='checkbox']";
        if ($(this).attr("checked") != "") {
            if ($(hhid).attr("checked") == false && $(bbid).attr("checked") == false) {
                $(hhid).parent().css("background-color", "plum");
                yanzheng = false;
            }
            else {
                $(hhid).parent().css("background-color", "");
            }
        }
    });
    if (yanzheng == false) {
        alert("请选择评定结论");
    }
    return yanzheng;
}

//建筑验证
function jzdata() {

    var jzdata = "";
    var yanzheng = true;
    var xiaoshu = ['txtJzgd', 'txtJzmj'];
    $("tr[id^='jztr']").each(function() {
        if ($(this).css("display") != "none") {
            $(this).children("td").each(function() {
                $(this).children("input").each(function() {
                    var id = $(this).attr("id").toString().Trim();
                    var subid = id.substr(id.search("txt"), id.length);
                    var bl = true;
                    switch (subid) {
                        case "txtGclbmc":
                        case "txtJzmj":
                        case "txtCs":
                        case "txtJzgd":
                            if ($(this).val().toString().Trim() == "") {
                                $(this).css("background-color", "plum");
                                yanzheng = false;
                                bl = false;
                            }
                            else {
                                jzdata += subid + "★" + $(this).val() + "☆";
                                $(this).css("background-color", "");
                            }
                            break;
                    }
                    if (bl == true) {
                        switch (subid) {
                            case "txtJzgd":
                                var mode = /^\d{1,8}(\.\d{1,2})?$/;
                                if (!mode.test($(this).val())) {
                                    $(this).css("background-color", "plum");
                                    yanzheng = false;
                                }
                                else {
                                    jzdata += subid + "★" + $(this).val() + "☆";
                                    $(this).css("background-color", "");
                                }
                                break;
                            case "txtZdmj":
                                if ($(this).val().Trim() != "") {
                                    var mode = /^\d{1,7}(\.\d{1,2})?$/;
                                    if (!mode.test($(this).val())) {
                                        $(this).css("background-color", "plum");
                                        yanzheng = false;
                                    }
                                    else {
                                        jzdata += subid + "★" + $(this).val() + "☆";
                                        $(this).css("background-color", "");
                                    }
                                }
                                else {
                                    jzdata += subid + "★" + $(this).val() + "☆";
                                    $(this).css("background-color", "");
                                }
                                break;
                            case "txtJzmj":
                                var mode = /^\d{1,7}(\.\d{1,2})?$/;
                                if (!mode.test($(this).val())) {
                                    $(this).css("background-color", "plum");
                                    yanzheng = false;
                                }
                                else {
                                    jzdata += subid + "★" + $(this).val() + "☆";
                                    $(this).css("background-color", "");
                                }
                                break;
                            default:
                                jzdata += subid + "★" + $(this).val() + "☆";
                                break;
                        }
                    }
                })
                $(this).children("select").each(function() {
                    var id = $(this).attr("id").toString().Trim();
                    var subid = id.substr(id.search("drop"), id.length);
                    $(this).children("option").each(function() {
                        if ($(this).attr("selected") != "") {
                            if ($(this).attr("value") != "#") {
                                jzdata += subid + "★" + $(this).attr("value") + "◇" + $(this).text() + "☆";
                            }
                            else {
                                jzdata += subid + "★◇☆";
                            }
                        }
                    })
                });
            });
        }
    });
    if (jzdata.length > 0) {
        jzdata = jzdata.substr(0, jzdata.length - 1);
    }
    if (yanzheng == false) {
        alert("建筑数据格式不正确");
    }
    $("#ctl00_MainContent_HiddenField1").val(jzdata);
    return yanzheng;
}

function ajaxyzwh() {
    var str = $("#ctl00_MainContent_HiddenField3").val().toString().split("|");
    var SSJG = str[0];
    var SSDW = str[1];
    var WSLB = str[2];
    var url = location.href;
    var ItemBH = "";
    var parstring = url.substring(url.indexOf("?") + 1, url.length).split("&");
    for (var i = 0; i < parstring.length; i++) {
        if (parstring[i].split("=")[0] == "ItemBH") {
            ItemBH = parstring[i].split("=")[1];
        }
    }
    var wh1 = "";
    var wh2 = "";
    var wh3 = $("#ctl00_MainContent_FLWSHead_txtYear").val();
    var wh4 = $("#ctl00_MainContent_FLWSHead_txtLSH").val();
    var message = "";
    var yangzheng = false;
    if (str.length > 3) {
        //修改文号判定
        var oldwh4 = str[3];
        var FlwsID = str[4];
        $.getJSON("AjaxHandler.ashx", { 'FlwsID': FlwsID, 'SSJG': SSJG, 'SSDW': SSDW, 'ItemBH': ItemBH, 'WSLB': WSLB, 'wh2': wh2, 'wh3': wh3, 'wh4': wh4, 'oldwh4': oldwh4 }, function(json) {
            if (json.Result != "") {
                alert(json.Result);
            }
            else {
                __doPostBack('ctl00$MainContent$btnSave', '');
            }

        });
    }
    else {
        //添加文号判定
        $.getJSON("AjaxHandler.ashx", { 'SSJG': SSJG, 'SSDW': SSDW, 'ItemBH': ItemBH, 'WSLB': WSLB, 'wh1': wh1, 'wh2': wh2, 'wh3': wh3, 'wh4': wh4 }, function(json) {
            if (json.Result != "") {
                alert(json.Result);
            }
            else {
                __doPostBack('ctl00$MainContent$btnSave', '');
            }

        });
    }
    return yangzheng;
}